home *** CD-ROM | disk | FTP | other *** search
- 'Note:
- ' You may have trouble running this program from the editor. If so,
- 'change the Path$ variable in INITIALISE to point to the directory this
- 'example is in. This isn't a problem when compiled.
-
- '**********************************************************************
- '*
- '* Os and Xs
- '*
- '**********************************************************************
-
- ' As a break from all the useless little utilities I've been writing,
- 'here is a useless little game.
- ' It demonstrates the use of AMOS Pro drawing commands on intuition, and
- 'more importantly, the use of the IDCMP_NEWSIZE flag. This flag allows
- 'us to redraw the board in a new size every time the user changes the size
- 'of the window. We also use some very simple menus.
- ' And no, there's no fancy artificial intelligence. It's two player only.
-
- '**********************************************************************
-
- Global _SCRAPTAGS,SCRAPTAGS,_PORTLIST,_MESSLIST
- Global PATH$,OSVER
- Global FHEIGHT,FWIDTH,MBAR,OX,OY,SW,SH
- Dim _OXGADS(0)
- Global _OXGADS()
- Global _OXMENU,_OXMENADD
- Global _OXWIND
-
- On Error Proc _CLEANUP
-
- '** _MOVE() contains various static strings for use as the window title
- ' _MYMOVE is 0 for Os to go, 1 for Xs
- Dim _MOVE(4)
- Global _MOVE(),_MYMOVE
- _MOVE(0)=J Make String("O's move")
- _MOVE(1)=J Make String("X's move")
- _MOVE(2)=J Make String("Os and Xs for AMOS")
- _MOVE(3)=J Make String("O wins!")
- _MOVE(4)=J Make String("X wins!")
-
- '** _BOARD() represents the board, 0=blank, 1=O, 2=X
- ' WIN holds the present winner 0=none, 1=O, 2=X
- ' XD,YD are the sizes, in pixels, of each square on the board.
- Dim _BOARD(2,2)
- Global _BOARD(),WIN
- Global XD,YD
-
- _INITIALIZE
- _GUIDATA
- _SETUPALL
- _SETPORTS
- Do
- K=J Wait Message
- While K
- C=J Tag Data(_MESSLIST,1)
- If C=Equ("IDCMP_CLOSEWINDOW")
- 'Bring up a requester before quiting
- If J Easy Request(_OXWIND,"Quit"," Are you ure you"+Chr$(10)+"want to quit?"," Quit | Cancel ",0)
- _CLEANUP
- End If
- Else If C=Equ("IDCMP_REFRESHWINDOW")
- _DOREFRESH
-
- Else If C=Equ("IDCMP_NEWSIZE")
- 'If the user resizes the window, redraw the board in a new size.
- _DRAW_BOARD
-
- Else If C=Equ("IDCMP_MOUSEBUTTONS") and J Tag Data(_MESSLIST,2)=Equ("SELECTUP") and WIN=0
- 'The user has tried to make a move.
- _HANDLE_MOVE
-
- Else If C=Equ("IDCMP_MENUPICK")
- 'Read the menus in the normal way. There's only one menu, so we
- 'only need to read the Item.
- C=J Tag Data(_MESSLIST,2)
- I=J Read Item(C)
-
- If I=0
- 'New Gane: Clear the board array, set the winner to 0, and
- 'change the window title
- For Y=0 To 2
- For X=0 To 2
- _BOARD(X,Y)=0
- Next X
- Next Y
- _DRAW_BOARD
- WIN=0
- J Set Window Titles _OXWIND,_MOVE(_MYMOVE),_MOVE(2)
-
- Else If I=2
- 'Bring up a requester before quiting
- If J Easy Request(_OXWIND,"Quit"," Are you ure you"+Chr$(10)+"want to quit?"," Quit | Cancel ",0)
- _CLEANUP
- End If
-
- End If
- End If
- K=J Next Message
- Wend
- Loop
-
- Procedure _DRAW_BOARD
- On Error Proc _CLEANUP
-
- 'First, get the inner size of the window, and split this into three
- 'parts
- J This Window _OXWIND
- XE=J Window Width-J Border Right-1
- YE=J Window Height-J Border Bottom-1
- XD=(XE-J X Offset)/3 : YD=(YE-J Y Offset)/3
-
- 'Clear the window - get rid of any rubbish that's there already.
- Ink 0
- Bar J X Offset,J Y Offset To XE,YE
-
- 'Draw the four lines across the window, according to the divisions
- 'we worked out above.
- Ink 1
- For I=1 To 2
- Draw J X Offset,J Y Offset+I*YD To XE,J Y Offset+I*YD
- Draw J X Offset+I*XD,J Y Offset To J X Offset+I*XD,YE
- Next I
-
- 'Now fill in the counters. The _RENDER_COUNTER procedure does the
- 'actual drawing.
- For Y=0 To 2
- For X=0 To 2
- If _BOARD(X,Y)
- _RENDER_COUNTER[X,Y,_BOARD(X,Y)]
- End If
- Next X
- Next Y
-
- End Proc
- Procedure _RENDER_COUNTER[X,Y,T]
- On Error Proc _CLEANUP
-
- 'X,Y square to draw the counter
- 'T type of counter 1=O 2=X
- Ink 2
-
- 'This is the radius of a counter. Each counter therefore occupies
- 'two thirds of the square. Also find the centre of the square
- RX=XD/3 : RY=YD/3
- CX=(X+0.5)*XD+J X Offset : CY=(Y+0.5)*YD+J Y Offset
-
- 'Draw in the appropriate counter from the above data.
- If RX>0 and RY>0
- If T=1
- Ellipse CX,CY,RX,RY
- Else
- Draw CX-RX,CY-RY To CX+RX,CY+RY
- Draw CX+RX,CY-RY To CX-RX,CY+RY
- End If
- End If
-
- End Proc
- Procedure _HANDLE_MOVE
- On Error Proc _CLEANUP
-
- 'Find the square the player wants to move to.
- X=(J Tag Data(_MESSLIST,5)-J X Offset)/XD
- Y=(J Tag Data(_MESSLIST,6)-J Y Offset)/YD
-
- 'Check it's actually on the board, and that the square is empty
- If X=>0 and X<=2 and Y=>0 and Y<=2
- If _BOARD(X,Y)=0
-
- 'place the counter in the BOARD array, and draw it. Change the move
- 'counter to the other players turn.
- _BOARD(X,Y)=_MYMOVE+1
- _RENDER_COUNTER[X,Y,_MYMOVE+1]
- Add _MYMOVE,1,0 To 1
-
- 'check for lines. If a line is found, WIN is set to the winning
- 'player, and the game is stopped until new game is selected.
- For I=0 To 2
- If _BOARD(0,I)=_BOARD(1,I) and _BOARD(1,I)=_BOARD(2,I) and _BOARD(0,I)<>0
- WIN=_BOARD(0,I)
- End If
- Next I
- For I=0 To 2
- If _BOARD(I,0)=_BOARD(I,1) and _BOARD(I,1)=_BOARD(I,2) and _BOARD(I,0)<>0
- WIN=_BOARD(I,0)
- End If
- Next I
- If _BOARD(0,0)=_BOARD(1,1) and _BOARD(1,1)=_BOARD(2,2) and _BOARD(0,0)<>0
- WIN=_BOARD(0,0)
- End If
- If _BOARD(2,0)=_BOARD(1,1) and _BOARD(1,1)=_BOARD(0,2) and _BOARD(2,0)<>0
- WIN=_BOARD(2,0)
- End If
-
- 'Change the window title to show whose turn or who won.
- If WIN=0
- J Set Window Titles _OXWIND,_MOVE(_MYMOVE),_MOVE(2)
- Else
- J Set Window Titles _OXWIND,_MOVE(2+WIN),_MOVE(2)
- End If
- End If
- End If
-
- End Proc
- 'procs below produced by Gadtools
-
- Procedure _INITIALIZE
- Procedure _SETUPALL
- Procedure _GUIDATA
- Procedure _MAKEOXGADS
- Procedure _MAKEOXWIND[SC]
- Procedure _DOREFRESH
- Procedure _SETPORTS
- Procedure _FREEWIND[W,G,M,A,C]
- Procedure _CLEANUP
-